package easy._110_BalancedBinaryTree;

public class BalancedBinaryTree {

    public int maxDepth(TreeNode root) {
        if(root==null){
            return  0;
        }else{
            int leftDepth,rightDepth = 0;
            return 1+(((leftDepth = maxDepth(root.left))>(rightDepth = maxDepth(root.right)))?leftDepth:rightDepth);
        }
    }

    public boolean isBalanced(TreeNode root) {
        if(root==null) return true;
        int length = (Math.abs(maxDepth(root.left)-maxDepth(root.right)));
        if(length>=2) return false;
        return isBalanced(root.left) && isBalanced(root.right);
    }
}
